package com.icim.whtm.exporter.service;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.icim.csot.exporter.tx.csotshipping.CsotShippingO;
import com.icim.dao.RetBoxDao;
import com.icim.dao.RetPrdInDao;
import com.icim.dao.RetWoDao;
import com.icim.entity.Ret_box;
import com.icim.entity.Ret_prd_in;
import com.icim.entity.Ret_wo;
import com.icim.util.TimeUtil;
import com.icim.whtm.dao.Ct_PpboxProductSumDao;
import com.icim.whtm.entity.Ct_PpboxProductSum;
import com.icim.whtm.exporter.controller.WhtmShippingdataController;

@Service
public class WhtmExportShippingDataService implements IWhtmExportShippingDataService {

	@Resource
	private Ct_PpboxProductSumDao ppboxproductsumdao;
	
	@Resource
	DataSourceTransactionManager whtmTransactionManager;
	
	@Resource
	private RetBoxDao retboxdao;
	
	@Resource
	private RetWoDao retwodao;
	
	@Resource
	private RetPrdInDao retproductin;
	
	private Logger logger = LoggerFactory.getLogger(WhtmExportShippingDataService.class);
	
	@Override
	public CsotShippingO sendShippingData(String palletId,String ppboxId,String flag)
			throws Exception {
		CsotShippingO outTrx = new CsotShippingO();
		
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		TransactionStatus transaction = whtmTransactionManager.getTransaction(def);
		
		Ct_PpboxProductSum ppboxproductsum = new Ct_PpboxProductSum();
		String currentTime = TimeUtil.getSixTimeBehindSecond();//当前时间,作为最近一次的事件时间
		Timestamp currentTime2 = TimeUtil.getCurrentTimestamp();//当前的时间，作为最近一次的操作时间
		//对flag进行判断，LS为来料收料，CH为出货
		if(flag.equals("LS"))
		{
			//产品来料收料，更新表数据
			try{
				//获取回传数据所需要的所有数据，保存在表里
					
					ppboxproductsum.setTimekey(currentTime);
					
					ppboxproductsum.setBoxstate("InUse");
					ppboxproductsum.setLocation("OutSourcing");
					
					Ret_box retbox = retboxdao.getBoxListByBoxId(ppboxId).get(0);
					if(retbox==null){
						logger.error("回传169数据失败，未找到箱号为"+ppboxId+"的箱子");
					}
					String boxname = retbox.getBoxrealid();//物理箱号
					Ret_prd_in retprdin = retproductin.getBoxListByBoxId(ppboxId).get(0);
					if(retprdin==null){
						logger.error("回传169数据失败，未找到箱号"+ppboxId+"的来料数据");
					}
					int glsqty = retprdin.getPrd_tl_qty();//玻璃数量
					String wo_id = retprdin.getWo_id_fk();//工单id
					String event_user = retprdin.getEvt_usr();//最近事件的操作人
					
					Ret_wo retwo = retwodao.getWo(wo_id).get(0);
					if(retwo==null){
						logger.error("回传169数据失败，未找到工单"+retwo+"的信息");
					}
					String prd_type = retwo.getMdl_id_fk();//产品型号
					
					ppboxproductsum.setBoxname(boxname);
					ppboxproductsum.setPpboxid(boxname);
					ppboxproductsum.setGlsqty(String.valueOf(glsqty));
					ppboxproductsum.setLotname(null);
					ppboxproductsum.setProductrequestname(retwo.getCus_info_snd());
					ppboxproductsum.setProductspecname(prd_type);
					ppboxproductsum.setLasteventname("OutSourcingReceive");
					ppboxproductsum.setLasteventtime(currentTime2);
					ppboxproductsum.setLasteventuser(event_user);
					ppboxproductsum.setLasteventcomment(null);
					ppboxproductsum.setReceiveflag("N");
					
					ppboxproductsumdao.save(ppboxproductsum);
					
					whtmTransactionManager.commit(transaction);
				} catch (Exception ex) {
					whtmTransactionManager.rollback(transaction);
					throw ex;
				}
			
		}
		else if(flag.equals("CH"))
		{
			List<Ret_box> boxList = retboxdao.getBoxList(palletId);
			//产品出货，更新CT_PPBOXPRODUCTSUM表
			try{
				for (Ret_box retbox : boxList) {
					
					ppboxproductsum.setTimekey(currentTime);
					logger.info("TimeKey: " ,currentTime);
					ppboxproductsum.setBoxstate("InUse");
					ppboxproductsum.setLocation("Transfer");
					
					
					String boxname = retbox.getBoxrealid();//物理箱号
					logger.info("Boxname: " ,boxname);
					int glsqty = retbox.getPrd_qty();//玻璃数量
					String wo_id = retbox.getWo_id_fk();//工单id
					String event_user = retbox.getEvt_usr();//最近事件的操作人
					
					Ret_wo retwo = retwodao.getWo(wo_id).get(0);
					if(retwo==null){
						logger.error("回传169出货数据失败，未找到工单"+retwo+"的信息");
					}
					String prd_type = retwo.getMdl_id_fk();//产品型号
					
					ppboxproductsum.setBoxname(boxname);
					ppboxproductsum.setPpboxid(boxname);
					ppboxproductsum.setGlsqty(String.valueOf(glsqty));
					ppboxproductsum.setLotname(null);
					ppboxproductsum.setProductrequestname(retwo.getCus_info_snd());
					ppboxproductsum.setProductspecname(prd_type);
					ppboxproductsum.setLasteventname("OutSourcingOut");
					ppboxproductsum.setLasteventtime(currentTime2);
					ppboxproductsum.setLasteventuser(event_user);
					ppboxproductsum.setLasteventcomment(null);
					ppboxproductsum.setReceiveflag("N");
					
					ppboxproductsumdao.save(ppboxproductsum);
					
				}
					whtmTransactionManager.commit(transaction);
				} catch (Exception ex) {
					whtmTransactionManager.rollback(transaction);
					throw ex;
				}
		}
		
		outTrx.setRtn_code("0000000");
		outTrx.setRtn_mesg("SUCCESS");
		return outTrx;
	}

}
